//
//  YJMOrigialViewStatus.swift
//  Weibo
//
//  Created by 杨军民 on 16/7/25.
//  Copyright © 2016年 杨军民. All rights reserved.
//


/*
//重点。Swift对语法要求高，所以以后要留意

1、懒加载控件，并且控件有多长相同，所以在扩展一个构造函数，代码显得简洁
2、塔尖UI界面，将其添加到UIView控制器中，并将其约束

数据所有在这里，所以要在这里获取到数据然后传值给控制器

3、获取微博模型对其控件赋值
4、提供给外界使用

*/
import UIKit
import SDWebImage

//原创view，显示原创的消息
class YJMOrigialViewStatus: UIView {
    
    //微博模型
    var startus: YJMWeiBoModel? {
        didSet{
            //头像
            if let profile_image_url = startus?.user?.profile_image_url{
                let url = NSURL(string: profile_image_url)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            //名称
            userName.text = startus?.user?.screen_name
            //时间
            timeLabel.text = startus?.created_at
            //来源
            sourceLabel.text = startus?.source
            //会员等级
            if let mbrank = startus?.user?.mbrank {
                if mbrank > 0 && mbrank <= 6 {
                    let image = UIImage(named: "common_icon_membership_level\(mbrank)")
                    memberImageView.image = image
                } else {
                    memberImageView.image = nil
                }
            }
            //认证类型
            if let verified_type = startus?.user?.verified_type {
                var image: UIImage? = nil
                
                switch verified_type {
                    //认证用户
                case 0 :image = UIImage(named: "avatar_vip")
                    //企业级认证
                case 2,3,5 :image = UIImage(named: "avatar_enterprise_vip")
                    //达人
                case 220:image = UIImage(named: "avatar_grassroot")
                default :
                    image = nil
                }
             verifiedImageView.image = image
            }
            //微博内容
            contentLabel.text = startus?.text
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //原创view
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //UI界面
        setUI()
    }
    
    //UI界面
    private func setUI() {
        //添加子控件
        self.addSubview(iconView)
        self.addSubview(userName)
        self.addSubview(timeLabel)
        self.addSubview(sourceLabel)
        self.addSubview(memberImageView)
        self.addSubview(verifiedImageView)
        self.addSubview(contentLabel)
        
        //添加约束
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(CellMargin)
            make.size.equalTo(CGSize(width: CellIconWH, height: CellIconWH))
        }
        
        //名称
        userName.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CellMargin)
            make.top.equalTo(iconView)
        }
        
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(userName)
            make.bottom.equalTo(iconView)
        }
        
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        //会员等级
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(userName.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(userName)
        }
        
        //认证图标
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        //微博文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(12)
        }
        
        //约束原创View和微博文本底部Label的和
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentLabel).offset(CellMargin)
        }
    }
    
    
    //MARK: - 懒加载
    //头像
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "avatar"))
    //用户名称
    private lazy var userName: UILabel = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
    
    //时间
    private lazy var timeLabel: UILabel = UILabel (color: UIColor.orangeColor())
    
    //来源
    private lazy var sourceLabel: UILabel = UILabel(color: UIColor.orangeColor())
    
    //会员等级
    private lazy var memberImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //认证图标
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //微博文本内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(),fontSize: 16)
        
        //设置多行
        label.numberOfLines = 0
        //文字换行的宽度
        label.preferredMaxLayoutWidth = UIScreen.width() - 2 * CellMargin
        
        return label
    }()
    
}
